package com.flying.arts.trade.controller;

import com.flying.arts.trade.domain.entity.Trade;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

@RestController
@EnableFeignClients
@RequestMapping("/v1/match")
public class MatchController {
    private Logger logger = LoggerFactory.getLogger(MatchController.class);

    @Autowired
    private TradeClient tradeClient;

    @PostMapping
    public ResponseEntity<Collection<Trade>> matching(@RequestBody String mode) {
//        logger.info(String.format("trade-service add() invoked: %s for %s", service.getClass().getName(), trade.getId()));
        try {
            Collection<Trade> trades = tradeClient.getAll();
//            service.add(trade);
            return new ResponseEntity<Collection<Trade>>(trades, HttpStatus.OK);
        } catch (Exception e) {
            logger.error("Exception raised add Restaurant REST Call {0}", e);
            return new ResponseEntity<>(HttpStatus.UNPROCESSABLE_ENTITY);
        }
    }
}

@FeignClient("trade-service")
interface TradeClient {
    @GetMapping(value = "/v1/trade")
    Collection<Trade> getAll();
}
